package cn.jbolt.wechat.admin.autoreply;

import cn.jbolt.core.api.JBoltApiBaseController;
import cn.jbolt.core.api.JBoltBaseController;
import cn.jbolt.core.base.JBoltMsg;
import cn.jbolt.core.common.config.JBoltUploadFolder;
import cn.jbolt.core.enumutil.JBoltEnum;
import cn.jbolt.core.permission.CheckPermission;
import cn.jbolt.core.permission.UnCheck;
import cn.jbolt.core.permission.UnCheckIfSystemAdmin;
import cn.jbolt.wechat.common.WechatReplyContentType;
import cn.jbolt.wechat.model.WechatAutoreply;
import cn.jbolt.wechat.model.WechatMpinfo;
import cn.jbolt.wechat.model.WechatReplyContent;
import cn.jbolt.wechat.permission.WechatPermissionKey;
import cn.jbolt.wechat.service.JBoltWechatAutoReplyService;
import cn.jbolt.wechat.service.JBoltWechatMpinfoService;
import cn.jbolt.wechat.service.JBoltWechatReplyContentService;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.JFinal;
import com.jfinal.core.Path;
import com.jfinal.core.paragetter.Para;
import com.jfinal.kit.Ret;
import com.jfinal.upload.UploadFile;

/**
 * 微信公众平台自动回复内容设置管理
 *
 * @ClassName: WechatAutoReplyAdminController
 * @author: JFinal学院-小木 QQ：909854136
 * @date: 2019年6月20日
 */
@UnCheckIfSystemAdmin
@Path("/api/admin/wechatAutoReply/content")
public class WechatReplyContentAdminController extends JBoltApiBaseController {
    @Inject
    private JBoltWechatAutoReplyService wechatAutoReplyService;
    @Inject
    private JBoltWechatReplyContentService service;
    @Inject
    private JBoltWechatMpinfoService wechatMpinfoService;


    @UnCheck
    public void index() {
        renderJBoltApiSuccessWithData(service.findById(getLong(0)));
    }

    @UnCheck
    @Before(WechatReplyContentMgrValidator.class)
    public void datas() {
        Long autoReplyId = getLong(0);
        Ret ret = service.checkPermission(autoReplyId);
        if (ret.isFail()) {
            renderJBoltApiRet(ret);
            return;
        }
		renderJBoltApiSuccessWithData(service.getListByAutoReplyId(autoReplyId));
    }


    @UnCheck
    @Before(WechatReplyContentMgrValidator.class)
    public void up() {
        renderJBoltApiRet(service.doUp(getLong(0), getLong(1)));
    }

    @UnCheck
    @Before(WechatReplyContentMgrValidator.class)
    public void down() {
		renderJBoltApiRet(service.doDown(getLong(0), getLong(1)));
    }

    @UnCheck
    @Before(WechatReplyContentMgrValidator.class)
    public void initRank() {
		renderJBoltApiRet(service.doInitRank(getLong(0)));
    }

    @UnCheck
    @Before(WechatReplyContentMgrValidator.class)
    public void delete() {
		renderJBoltApiRet(service.delete(getLong(0), getLong(1)));
    }

    @UnCheck
    @Before(WechatReplyContentMgrValidator.class)
    public void toggleEnable() {
		renderJBoltApiRet(service.toggleEnable(getLong(0), getLong(1)));
    }

    @UnCheck
    public void types() {
        renderJBoltApiSuccessWithData(JBoltEnum.getEnumOptionList(WechatReplyContentType.class));
    }

    /**
     * 上传公众平台相关图片
     */
    @UnCheck
    @Before(WechatReplyContentMgrValidator.class)
    public void uploadImage() {
        Long autoReplyId = getLong(0);
        Ret ret = service.checkPermission(autoReplyId);
        if (ret.isFail()) {
            renderJBoltApiFail(ret.getStr("msg"));
            return;
        }
        //上传到今天的文件夹下
        String todayFolder = JBoltUploadFolder.todayFolder();
        String uploadPath = JBoltUploadFolder.WECHAT_AUTOREPLY_REPLYCONTENT + "/" + todayFolder;
        UploadFile file = getFile("img", uploadPath);
        if (notImage(file)) {
            renderJsonFail("请上传图片类型文件");
            return;
        }
        renderJBoltApiSuccessWithData(JFinal.me().getConstants().getBaseUploadPath() + "/" + uploadPath + "/" + file.getFileName());
    }

    @UnCheck
    public void save(@Para("autoReplyId") Long autoReplyId, @Para("wechatReplyContent") WechatReplyContent content) {
		renderJBoltApiRet(service.save(autoReplyId, content));
    }

    @UnCheck
    public void update(@Para("autoReplyId") Long autoReplyId, @Para("wechatReplyContent") WechatReplyContent content) {
		renderJBoltApiRet(service.update(autoReplyId, content));
    }

}
